package screen;

import main.GameCanvas;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import utilities.CRes;
import utilities.GameResource;

import model.Command;
import model.Position;
import font.BitmapFont;

public class Menu {
	public Vector m_list;
	
	public int m_selectedIndex;//, chan;
	public int m_left, m_top, m_menuW, menuH, menuTemY;
	
	public boolean m_showMenuGarden = false;
	public boolean m_showMenu;
	public boolean m_bIsShowCustomPos = false;
	
	public int cmtoX, cmx, cmdx, cmvx, cmxLim;
	public int cmtoY, cmy, cmdy, cmvy, cmyLim;
	
	private int xL;
	private int size = 0;
	
	int pa = 0;
	int m_iDeltaY = 10;
	
	public void startMenuGarden(Vector menuItem, int x, int y, int w) {
		xL = GameCanvas.h;
		m_showMenuGarden = true;
		m_showMenu = true;
		m_left = x;
		m_top = y;
		m_menuW = w;
		
		CRes.cleanVector(m_list);
		m_list = menuItem;
		size = menuItem.size();
		if (m_selectedIndex >= size)
			m_selectedIndex = 0;
		
		if (GameCanvas.w < 200)
			cmxLim = (size * m_menuW + 20) - GameCanvas.hw - 3 * m_menuW;
		else if (GameCanvas.w < 300)
			cmxLim = (size * m_menuW + 20) - GameCanvas.hw - 4 * m_menuW;
		else
			cmxLim = (size * m_menuW + 20) - GameCanvas.hw - 6 * m_menuW;
		if (cmxLim < 0)
			cmxLim = 0;
	}
	
	public void startAt(Vector menuList, int pos) {
		xL = GameCanvas.h;
		//chan = 0;
		CRes.cleanVector(m_list);
		m_list = menuList;
		size = m_list.size();
		m_menuW = menuH = 0;
		int i;
		for (i = 0; i < size; i++) {
			Command c = (Command) m_list.elementAt(i);
			int w = BitmapFont.m_bmFont.stringWidth(c.caption);
			
			if (w > m_menuW)
				m_menuW = w;
			menuH += Screen.ITEM_HEIGHT;
		}
		m_menuW += 10;
		if (m_menuW < 100)
			m_menuW = 100;
		menuH += 4;
		if (pos == 0)
			m_left = 2;
		else if (pos == 1)
			m_left = GameCanvas.w - m_menuW - 2;
		else {
			m_left = (GameCanvas.hw) - (m_menuW >> 1);
		}
		if (size > 5) {
			menuH = Screen.ITEM_HEIGHT * 5 + 4;
		}
		m_top = GameCanvas.h - GameCanvas.hBottomBar - menuH;
		if (GameCanvas.h < 200) {
			m_top += 10;
		}
		
		menuTemY = GameCanvas.h - Screen.ITEM_HEIGHT;
		m_showMenu = true;
		m_showMenuGarden = false;
		m_bIsShowCustomPos = false;
		m_selectedIndex = 0;
		cmyLim = size * Screen.ITEM_HEIGHT - Screen.ITEM_HEIGHT * 5;
		if (cmyLim < 0)
			cmyLim = 0;
		cmtoY = 0;
		cmy = 0;
	}

	public void updateMenuKey() {
		if (m_showMenuGarden) {
			updateMenuFarmKey();
		} else
			updateMenuKeyMain();
		
		if (GameCanvas.keyPressed[5] || GameCanvas.keyPressed[12]) {
			m_showMenu = false;
			GameCanvas.keyPressed[5] = false;
			GameCanvas.keyPressed[12] = false;
			Command cmd = ((Command) m_list.elementAt(m_selectedIndex));
			cmd.action.perform();
		} else if (GameCanvas.keyPressed[13]) {
			m_showMenu = false;
			m_showMenuGarden = false;
			GameCanvas.keyPressed[13] = false;
		}
		if (GameCanvas.isPointerClick){
			for (int i = 0; i < 3; i++) {
				if (GameCanvas.isPointer(Screen.posCmd[i].x, Screen.posCmd[i].y,
						40, 22)) {
					m_showMenu = false;
					
					if (i < 2)
						((Command) m_list.elementAt(m_selectedIndex)).action.perform();
				}
			}
		}
	}

	private void updateMenuFarmKey() {
		boolean changeFocus = false;
		if (GameCanvas.keyPressed[4]) {
			GameCanvas.keyPressed[4] = false;
			m_selectedIndex--;
			if (m_selectedIndex < 0)
				m_selectedIndex = size - 1;
			
			changeFocus = true;
		} else if (GameCanvas.keyPressed[6]) {
			GameCanvas.keyPressed[6] = false;
			m_selectedIndex++;
			if (m_selectedIndex >= size)
				m_selectedIndex = 0;
			
			changeFocus = true;
		}

		if (GameCanvas.isPointerDown) {
			if (GameCanvas.isPointer((m_left - m_menuW * size / 2), m_top, m_menuW* size, m_menuW + 9)) {
				pa = GameCanvas.pxLast - GameCanvas.px;
				int aa = (m_list.size() * m_menuW / (GameCanvas.w) * 2);
				if (aa != 0)
					pa *= aa;
			}
		}
		if (GameCanvas.isPointerClick) {
			if (GameCanvas.isPointer((m_left - m_menuW * size / 2), m_top,
					m_menuW * size, m_menuW + 9)) {
				cmtoX = cmx + pa;
				if (cmtoX < -30)
					cmtoX = -30;
				if (cmtoX > cmxLim + 30)
					cmtoX = cmxLim + 30;

				if (pa == 0)
					changeFocus = true;

				int bb = 0;
				if(cmxLim>0){
					if (GameCanvas.w < 300) {
						if(size%2==0)
							bb = (cmtoX + GameCanvas.px - (m_left - m_menuW * size / 2) + m_menuW/2)			
								/ m_menuW;
						else
							bb = (cmtoX + GameCanvas.px - (m_left - m_menuW * size / 2) + m_menuW)
							/ m_menuW;
					}else{
						if(size%2==0)
							bb = (cmtoX + GameCanvas.px - (m_left - m_menuW * size / 2) + m_menuW/2 + 10)			
								/ m_menuW;
						else
							bb = (cmtoX + GameCanvas.px - (m_left - m_menuW * size / 2) + 10)			
							/ m_menuW;
					}
				}else{
					bb = (cmtoX + GameCanvas.px - (m_left - m_menuW * size / 2))			
							/ m_menuW;
				}
				if (GameCanvas.w > 300) {
					if (cmxLim > 20) {
						if (size <= 11)
							bb -= 1;
						else if (size <= 13)
							bb -= 2;
						else if (size <= 15)
							bb -= 3;
						else if (size <= 17)
							bb -= 4;
						else if (size <= 19)
							bb -= 5;
						else if (size <= 21)
							bb -= 6;
						else if (size <= 23)
							bb -= 7;
						else if (size <= 25)
							bb -= 8;
						else if (size <= 27)
							bb -= 9;
						else if (size <= 29)
							bb -= 10;
						else if (size <= 31)
							bb -= 11;
						else if (size <= 33)
							bb -= 12;
						else if (size <= 35)
							bb -= 13;
						else if (size <= 37)
							bb -= 14;
						else if (size <= 39)
							bb -= 15;
						else
							bb -= 16;
					}
				} else {
					if (cmxLim > 0) {
						if (size < 10)
							bb -= 1;
						else if (size == 10)
							bb -= 2;
						else if(size<=12)
							bb -=3;
						else if (size <= 14)
							bb -= 4;
						else if(size<=16)
							bb -=5;
						else if(size<=18)
							bb -=6;
						else if(size<=20)
							bb -=7;
						else if(size<=22)
							bb -=8;
						else if(size<=24)
							bb -=9;
						else if(size<=26)
							bb -=10;
						else if(size<=28)
							bb -=11;
						else if(size<=30)
							bb -=12;
						else if(size<=32)
							bb -=13;
						else if(size<=34)
							bb -=14;
						else if(size<=36)
							bb -= 15;
						else if(size<=38)
							bb -= 16;
						else if(size<=40)
							bb -= 17;
						else
							bb-=18;
					}
				}

				if (m_selectedIndex == bb) {
					m_showMenu = false;
					m_showMenuGarden = false;
					((Command) m_list.elementAt(m_selectedIndex)).action
							.perform();
				}
				m_selectedIndex = bb;
				if (m_selectedIndex < 0)
					m_selectedIndex = 0;
				if (m_selectedIndex >= m_list.size())
					m_selectedIndex = m_list.size() - 1;
			}
		}
		if (changeFocus && size * m_menuW > GameCanvas.w) {
			cmtoX = m_selectedIndex * m_menuW - GameCanvas.hw + m_menuW;
			if (cmtoX > cmxLim)
				cmtoX = cmxLim;
			if (cmtoX < 0)
				cmtoX = 0;
		}
	}

	private void updateMenuKeyMain() {
		boolean changeFocus = false;
		if (GameCanvas.keyPressed[2] || GameCanvas.keyPressed[4]) {
			changeFocus = true;
			GameCanvas.keyPressed[2] = false;
			GameCanvas.keyPressed[4] = false;
			m_selectedIndex--;
			if (m_selectedIndex < 0)
				m_selectedIndex = size - 1;
		} else if (GameCanvas.keyPressed[8] || GameCanvas.keyPressed[6]) {
			changeFocus = true;
			GameCanvas.keyPressed[8] = false;
			GameCanvas.keyPressed[6] = false;
			m_selectedIndex++;
			if (m_selectedIndex > size - 1)
				m_selectedIndex = 0;
		}

		if (GameCanvas.isPointer(m_left - 2, menuTemY - 7, m_menuW + 4, menuH + 15)) {
			if (GameCanvas.isPointerDown) {
				pa = GameCanvas.pyLast - GameCanvas.py;
				int aa = (m_list.size() * Screen.ITEM_HEIGHT / ((menuH + 15) * 2));
				if (aa != 0)
					pa *= aa;
			}
			if (GameCanvas.isPointerClick) {
				cmtoY = cmy + pa;
				if (cmtoY < 0)
					cmtoY = 0;
				if (cmtoY > cmyLim)
					cmtoY = cmyLim;
				int b = menuTemY;
				if (pa == 0)
					changeFocus = true;
				int aa = (cmtoY + GameCanvas.py - b) / Screen.ITEM_HEIGHT;
				if (m_selectedIndex == aa && pa == 0) {
					m_showMenu = false;
					((Command) m_list.elementAt(m_selectedIndex)).action.perform();
				}
				m_selectedIndex = aa;
				if (m_selectedIndex < 0)
					m_selectedIndex = 0;
				if (m_selectedIndex >= m_list.size())
					m_selectedIndex = m_list.size() - 1;
				}
		}
		if (changeFocus) {
			cmtoY = m_selectedIndex * Screen.ITEM_HEIGHT - 2 * Screen.ITEM_HEIGHT;
			if (cmtoY > cmyLim)
				cmtoY = cmyLim;
			if (cmtoY < 0)
				cmtoY = 0;
			if (m_selectedIndex == size - 1 || m_selectedIndex == 0)
				cmy = cmtoY;
		}
	}

	public void paintMenu(Graphics g) {
		g.translate(xL, 0);
		
		if (m_showMenuGarden)
			paintMenuFarm(g);
		else
			paintMain(g);
	}

	private void paintMenuFarm(Graphics g) {
		g.translate((m_left - m_menuW * size / 2), 0);
		if (size * m_menuW > GameCanvas.w)
			g.translate((m_menuW * size / 2 - GameCanvas.hw + 8), 0);
		g.translate(-cmx, 0);

		Command com = (Command) m_list.elementAt(m_selectedIndex);
		BitmapFont.drawOutlinedString(g, com.caption, +m_selectedIndex * m_menuW + 11, m_top - 20, 0xffff00, 0x000000, (m_selectedIndex == 0)? Graphics.LEFT: Graphics.HCENTER);

		Position pos = GameCanvas.getMinMaxFor(cmx - 10, m_menuW, size, 1, GameCanvas.w + 20);
		int deltaY = 0;
		for (int i = pos.x; i < pos.y; i++) {
			deltaY = 0;
			if(i == m_selectedIndex)
				deltaY = m_iDeltaY;
			Command cmd = (Command) m_list.elementAt(i);
			cmd.paint(g, i * m_menuW + m_menuW / 2, m_top + 4 + m_menuW / 2 - deltaY);
		}
			
		pos = null;
		g.translate(-g.getTranslateX(), -g.getTranslateY());
		
		PaintPopup.paintBottomBar(g);

		BitmapFont.drawBoldFont(g, GameResource.no, GameCanvas.w - 5, GameCanvas.h - 18, 0xffffff, Graphics.RIGHT);
		BitmapFont.drawBoldFont(g, GameResource.select, GameCanvas.hw, GameCanvas.h - 17, 0xffffff, Graphics.HCENTER);
	}

	public void paintMain(Graphics g) {
		if (size == 0)
			return;

		g.translate(-g.getTranslateX(), -g.getTranslateY());

		paintMenuNormal(g);

		g.translate(-g.getTranslateX(), -g.getTranslateY());
		g.setClip(0, 0, GameCanvas.w, GameCanvas.h);

		PaintPopup.paintBottomBar(g);

		if(m_bIsShowCustomPos){
			BitmapFont.drawBoldFont(g, GameResource.select, GameCanvas.hw, GameCanvas.h - 17, 0xffffff, Graphics.HCENTER);	
		}
		else
		{
			BitmapFont.drawBoldFont(g, GameResource.no, GameCanvas.w - 5, GameCanvas.h - 18, 0xffffff, Graphics.RIGHT);
			BitmapFont.drawBoldFont(g, GameResource.select, 15, GameCanvas.h - 17, 0xffffff, Graphics.LEFT);	
		}
	}

	public void setCustomPos(int x, int y){
		m_left = x;
		m_top = y;
		m_bIsShowCustomPos = true;
	}
	
	public void paintMenuNormal(Graphics g) {
		PaintPopup.paintBox(g, m_left - 2, menuTemY - 7, m_menuW + 4, menuH + 15);

		g.translate(m_left + 3, menuTemY + 1);
		g.setClip(0, 0, m_menuW - 4, menuH - 2);
		g.translate(0, -cmy);

		String str;
		for (int i = 0; i < size; i++) {
			str = ((Command) m_list.elementAt(i)).caption;
			if (i == m_selectedIndex) {
				g.setColor(0x99ffaa);
				g.fillRect(-1, i * Screen.ITEM_HEIGHT + 1, m_menuW - 5, Screen.ITEM_HEIGHT);
				BitmapFont.drawBoldFont(g, str, 3, 4 + i * Screen.ITEM_HEIGHT, 0x3a001e, 0);
			}
			else
				BitmapFont.drawBoldFont(g, str, 3, 4 + i * Screen.ITEM_HEIGHT, 0xfdf8f7, 0);	
		}
	}

	public void updateMenu() {
		if (xL != 0)
			xL += (-xL >> 1);
		if (xL == -1)
			xL = 0;
		moveCamera();
		updateMain();
	}

	public void moveCamera() {
		if (cmx != cmtoX) {
			cmvx = (cmtoX - cmx) << 2;
			cmdx += cmvx;
			cmx += cmdx >> 4;
			cmdx = cmdx & 0xf;
		}
		if (cmy != cmtoY) {
			cmvy = (cmtoY - cmy) << 2;
			cmdy += cmvy;
			cmy += cmdy >> 4;
			cmdy = cmdy & 0xf;
		}
	}

	private void updateMain() {
		if (m_showMenuGarden) {
			if (Math.abs(cmtoX - cmx) < 15 && cmx < 0) {
				cmtoX = 0;
			}
			if (Math.abs(cmtoX - cmx) < 10 && cmx > cmxLim) {
				cmtoX = cmxLim;
			}
		}
		if (menuTemY > m_top) {
			int delta = ((menuTemY - m_top) >> 2);
			if (delta < 1)
				delta = 1;
			menuTemY -= delta;
		}
		menuTemY = m_top;
	}
}

